package com.jh3502.vivo.function.reimnew;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.jh3502.vivo.R;
import com.jh3502.vivo.function.reimnew.beans.CategoryInfoBean;
import com.jh3502.vivo.function.reimnew.beans.DetailBean;
import com.jh3502.vivo.view.MyRecyclerView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * author:harisucici on 2018/11/14 11:33
 * email:harisucici@gmail.com
 * name:
 * todo:
 */
public class ReimNewAdapter extends RecyclerView.Adapter<ReimNewAdapter.ViewHolder> implements View.OnClickListener {

    Context mContext;

    private View convertView;
    private OnRecyclerViewItemClickListener mOnItemClickListener = null;
    private ButtonInterface addReim, choosePlace;
    private List<DetailBean> dataList;
    private ReimNewItemAdapter mAdapter;
    private ReimNewItemInterface reimNewItemInterface;
    private double taxi;
    private double zsfAll, czcfAll;

    public ReimNewAdapter(Context context, List<DetailBean> dataList) {
        this.mContext = context;
        this.dataList = dataList;
    }

    public void setReimNewItemInterface(ReimNewItemInterface reimNewItemInterface) {
        this.reimNewItemInterface = reimNewItemInterface;
    }

    public void setAddReim(ButtonInterface buttonInterface) {
        this.addReim = buttonInterface;
    }


    public void setChoosePlace(ButtonInterface buttonInterface) {
        this.choosePlace = buttonInterface;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        convertView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_reim_new, viewGroup, false);
        ViewHolder vh = new ViewHolder(convertView);
        convertView.setOnClickListener(this);
        return vh;
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, final int position) {
        viewHolder.itemView.setTag(position);
        viewHolder.newAdd.setOnClickListener(arg0 -> {
            // TODO Auto-generated method stub
            if (addReim != null) {
                addReim.onClick(position);
            }
        });
        viewHolder.newPlace.setOnClickListener(arg0 -> {
            // TODO Auto-generated method stub
            if (choosePlace != null) {
                choosePlace.onClick(position);
            }
        });
        CategoryInfoBean cb = dataList.get(position).getCategoryInfo();
        viewHolder.newDate.setText(dataList.get(position).getDate());
        viewHolder.newPlace.setText(dataList.get(position).getCityname());
        mAdapter = new ReimNewItemAdapter(mContext, cb);
        mAdapter.setReimNewItemInterface(reimNewItemInterface);
        mAdapter.setTaxi(taxi);
        mAdapter.setHotel(dataList.get(position).getMoneyZsf());
        mAdapter.setPositionMain(position);
        viewHolder.newRecycler.setAdapter(mAdapter);
        viewHolder.newTotalApply.setText(dataList.get(position).getApplyMoney() + "");
        viewHolder.newTotalOut.setText(dataList.get(position).getOutMoney() + "");
        viewHolder.newTotalActual.setText(dataList.get(position).getActualMoney() + "");


    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {

            mOnItemClickListener.onItemClick((int) v.getTag());
        }
    }

    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    public void setTaxi(double taxi) {
        this.taxi = taxi;
    }

    public interface ButtonInterface {
        void onClick(int position);
    }


    public interface OnRecyclerViewItemClickListener {
        void onItemClick(int i);
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.new_date)
        TextView newDate;
        @BindView(R.id.new_place)
        TextView newPlace;
        @BindView(R.id.new_add)
        Button newAdd;
        @BindView(R.id.new_total_apply)
        TextView newTotalApply;
        @BindView(R.id.new_total_out)
        TextView newTotalOut;
        @BindView(R.id.new_total_actual)
        TextView newTotalActual;
        @BindView(R.id.new_recycler)
        MyRecyclerView newRecycler;

        public ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);

        }
    }

}
